import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/engagement/engagement_model.dart';

const _pageSize = 10;

class EngagementStationMorePageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  List<EngagementModel> engagementList = List<EngagementModel>.of([]);
  var stationId = 0;
  var stationName = '';

  @override
  void onInit() {
    if (Get.parameters.isNotEmpty) {
      stationId = int.tryParse(Get.parameters['stationId'] ?? '') ?? 0;
      stationName = Get.parameters['stationName'] ?? '';
    }
    super.onInit();
  }

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();
    final resp = await Api.getStationMoreList(
      stationId: stationId,
      page: page,
      pageSize: _pageSize,
    );

    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    final resp = await Api.getStationMoreList(
      stationId: stationId,
      page: page,
      pageSize: _pageSize,
    );

    if (resp == null) {
      return IndicatorResult.fail;
    }
    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();
    return IndicatorResult.success;
  }

  void _setList(List<EngagementModel>? list) {
    engagementList.clear();
    _addList(list);
  }

  void _addList(List<EngagementModel>? list) {
    if (null != list && list.isNotEmpty) {
      engagementList.addAll(list);
      update();
    }
  }
}
